package zelix.hack.hacks;

import zelix.hack.Hack;
import zelix.hack.HackCategory;
import zelix.value.NumberValue;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.init.Items;
import net.minecraft.inventory.ClickType;
import net.minecraft.item.ItemSoup;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.gameevent.TickEvent.PlayerTickEvent;
import zelix.utils.Utils;
import zelix.utils.Wrapper;

public class AutoSoup extends Hack {
	NumberValue health;
	private int oldSlot = -1;
	
	public AutoSoup() {
		super("AutoSoup", HackCategory.COMBAT);
		health=new NumberValue("Health", 5d, 2d, 9.5d);
		
		this.addValue(health);
	}
	

	@Override
	public void onPlayerTick(PlayerTickEvent event) {
		Utils.nullCheck();
		for(int i = 0; i < 36; i++)
		{
			// filter out non-bowl items and empty bowl slot
			ItemStack stack = Wrapper.INSTANCE.player().inventory.getStackInSlot(i);
			if(stack == null || stack.getItem() != Items.BOWL || i == 9)
				continue;
			
			// check if empty bowl slot contains a non-bowl item
			ItemStack emptyBowlStack =Wrapper.INSTANCE.player().inventory.getStackInSlot(9);
			boolean swap = !emptyBowlStack.isEmpty()
				&& emptyBowlStack.getItem() != Items.BOWL;
			
			// place bowl in empty bowl slot
			Wrapper.INSTANCE.controller().windowClick(0, i < 9 ? i + 36 : i, 0, ClickType.PICKUP, Wrapper.INSTANCE.player());
			Wrapper.INSTANCE.controller().windowClick(0, 9, 0, ClickType.PICKUP, Wrapper.INSTANCE.player());
			// place non-bowl item from empty bowl slot in current slot
			if(swap)
			Wrapper.INSTANCE.controller().windowClick(0, i < 9 ? i + 36 : i, 0, ClickType.PICKUP, Wrapper.INSTANCE.player());
		}
		int soupInHotbar = findSoup(0, 9);
		
		// check if any soup was found
		if(soupInHotbar != -1)
		{
			// check if player should eat soup
			if(!shouldEatSoup())
			{
				stopIfEating();
				return;
			}
			
			// save old slot
			if(oldSlot == -1)
				oldSlot = Wrapper.INSTANCE.player().inventory.currentItem;
			
			// set slot
			Wrapper.INSTANCE.player().inventory.currentItem = soupInHotbar;
			
			// eat soup
			KeyBinding.setKeyBindState(Wrapper.INSTANCE.mcSettings().keyBindUseItem.getKeyCode(), true);
		
			
			return;
		}
		
		stopIfEating();
		
		// search soup in inventory
		int soupInInventory = findSoup(9, 36);
		
		// move soup in inventory to hotbar
		if(soupInInventory != -1)
			Wrapper.INSTANCE.controller().windowClick(0, soupInInventory, 0, ClickType.QUICK_MOVE, Wrapper.INSTANCE.player());
		
		
		super.onPlayerTick(event);
	}
	
	
	private int findSoup(int startSlot, int endSlot)
	{
		for(int i = startSlot; i < endSlot; i++)
		{
			ItemStack stack = Wrapper.INSTANCE.player().inventory.getStackInSlot(i);
			
			if(stack != null && stack.getItem() instanceof ItemSoup)
				return i;
		}
		
		return -1;
	}
	private boolean shouldEatSoup()
	{
		// check health
		if(Wrapper.INSTANCE.player().getHealth() > health.getValue().floatValue() * 2F)
			return false;
		
		return true;
	}
	private void stopIfEating()
	{
		// check if eating
		if(oldSlot == -1)
			return;
		
		// stop eating
		KeyBinding.setKeyBindState(Wrapper.INSTANCE.mcSettings().keyBindUseItem.getKeyCode(), false);
		
		// reset slot
		Wrapper.INSTANCE.player().inventory.currentItem = oldSlot;
		oldSlot = -1;
	}
}
